package com.cssy.xxkj.logic.web;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONArray;
import com.cssy.authority.service.IScUserRolewhPlaceService;
import com.cssy.base.web.BaseController;
import com.cssy.base.web.BodyMsg;
import com.cssy.xxkj.basic.entity.BasicStorageInfoTestDO;
import com.cssy.xxkj.basic.service.IBasicUtilService;
import com.cssy.xxkj.logic.contants.BillStatusUtil;
import com.cssy.xxkj.logic.entity.LogicAllocatInwhDO;
import com.cssy.xxkj.logic.entity.LogicAllocatInwhItemDO;
import com.cssy.xxkj.logic.entity.LogicAllocatOutwhDO;
import com.cssy.xxkj.logic.entity.LogicAllocatOutwhItemDO;
import com.cssy.xxkj.logic.service.ILogicAllocatInwhItemService;
import com.cssy.xxkj.logic.service.ILogicAllocatInwhService;
import com.cssy.xxkj.logic.service.ILogicAllocatOutwhItemService;
import com.cssy.xxkj.logic.service.ILogicAllocatOutwhService;
import com.cssy.xxkj.logic.service.ILogicUtilService;
import com.cssy.yd.logic.util.SqlUtil;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchCondition;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.OperationFlag;
import com.plat.util.json.AjaxUtil;
import com.plat.util.json.OutJsonGrid;
/**
 * 调拨接收
 * @author zeyong.gui
 *
 */
@Controller
@RequestMapping("/logic/allocatinwh")
public class AllocatInwhController extends BaseController<LogicAllocatInwhDO> {
	
	private static final String MANAGER= "/xxkj/logic/allocat/ManagerEnd";
	private static final String EIDT = "/xxkj/logic/allocat/OperationEnd";
	
	@Resource(name = "basicUtilService")
	IBasicUtilService basicUtilService;
	
	@Resource(name = "logicUtilService")
	ILogicUtilService logicUtilService;
	
	@Resource(name = "logicAllocatOutwhService")
	ILogicAllocatOutwhService logicAllocatOutwhService;
	
	@Resource(name = "logicAllocatOutwhItemService")
	ILogicAllocatOutwhItemService logicAllocatOutwhItemService;
	
	@Resource(name = "logicAllocatInwhService")
	ILogicAllocatInwhService logicAllocatInwhService;
	
	@Resource(name = "logicAllocatInwhItemService")
	ILogicAllocatInwhItemService logicAllocatInwhItemService;
	
	@Resource(name="scUserRolePlaceService")
	private IScUserRolewhPlaceService scUserRolePlaceService;
	
	@Override
	public String manager(HttpServletRequest request, Model model)
			throws Exception {
			JqGridHandler hander = new JqGridHandler(request);
			SearchConditions scs = new SearchConditions();
			hander.getWheres(scs);
			scs.setRemovedProperty("removed");
			//发起仓库
			List<BasicStorageInfoTestDO> list = basicUtilService.resultStorage(scs);
			model.addAttribute("wh1",list);
			//接收仓
			model.addAttribute("wh",SqlUtil.sqlUtil.roleWH(request, scUserRolePlaceService));
		return MANAGER;
	}

	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		SearchResult result = logicAllocatInwhService.findListByExample(scs);
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);
	}

	@Override
	public String operationForm(HttpServletRequest request, @PathVariable String id,
			@PathVariable String flag, Model model) throws Exception {
		if (OperationFlag.ADD.equals(flag)) {
			model.addAttribute("flag", "ADD");
		} else {
			LogicAllocatInwhDO inwh = logicAllocatInwhService.findListById(id);
			model.addAttribute("inwh", inwh);
		}
		//查询调拨发起单号
		SearchConditions outwhScs = new SearchConditions();
		outwhScs.addCondition("zd002Status", BillStatusUtil.OUTWH_BILL_STATUS_FOUR, SearchCondition.OP_EQUAL);
		List<LogicAllocatOutwhDO> outwhList = logicUtilService.resultAlloactOutwh(outwhScs);
		model.addAttribute("outwhList", outwhList);
		return EIDT;
	}

	@Override
	public Map<String, Object> save(HttpServletRequest request,
			LogicAllocatInwhDO curAP, BindingResult result, Model model)
			throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		try{
			String dataItem = request.getParameter("dataItem");
			if(StringUtils.isBlank(dataItem)){
				bodyMap.isFail();
				bodyMap.configWebInfor("产品列表不能为空！");
				return bodyMap.modelMap;
			}
			logicAllocatInwhService.allocatInwhSave(request, curAP);
			bodyMap.isSuccess();
			bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
		}catch(Exception e){
			e.printStackTrace();
			bodyMap.isFail();
			bodyMap.configWebInfor(GlobalResLoader.getOpfailed()+ e.getMessage());
		}
		return bodyMap.modelMap;
	}

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		try {
			String ids = request.getParameter("ids");
			String[] fields=ids.split(",");
			for(String field : fields){
				if(StringUtils.isBlank(field)){
					continue;
				}else{
					logicAllocatInwhService.deleteBizRemoved(field);
				}
			}
			bodyMap.isSuccess(GlobalResLoader.getOpsuccess());
		} catch (Exception e) {
			e.printStackTrace();
			bodyMap.isFail(GlobalResLoader.getOpfailed() + e.getMessage());
		}
		return bodyMap.modelMap;
	}
	
	/**
	 * 查询采购入库单的行记录
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/gridItems")
	public void gridItems(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String aocInwhId = request.getParameter("aocInwhId");
		if(StringUtils.isNotBlank(aocInwhId)){
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("aocInwhId", aocInwhId);
			List<LogicAllocatInwhItemDO> aocInwhItemList = logicAllocatInwhItemService.findListByMap(paramMap);
			OutJsonGrid out = new OutJsonGrid(aocInwhItemList);
			out.printJSon(response);
		}
	}
	

	/**
	 * 根据调拨发起单ID取得调拨发起
	 * @param response
	 * @param gysCode
	 */
	@RequestMapping("/getAclOutwh")
	public void getAclOutwh(HttpServletResponse response,@RequestParam("outwhId") String outwhId){
		if(StringUtils.isNotBlank(outwhId)){
			LogicAllocatOutwhDO aclOutwh =  logicAllocatOutwhService.findListById(outwhId);
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("aclOutwh", aclOutwh);
			AjaxUtil.printJSonArr(response, JSONArray.toJSONString(map));
		}
	}
	
	/**
	 * 根据调拨单查询调拨订单行记录(allocatoutwhItem)信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/aochItems")
	public void aochItems(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String aochId = request.getParameter("aochId");
		if(StringUtils.isNotBlank(aochId)){
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("actoutwhId", aochId);
			List<LogicAllocatOutwhItemDO> outItemList = logicAllocatOutwhItemService.findListByMap(paramMap);
			if(outItemList==null||outItemList.isEmpty()||outItemList.size()==0){
				return;
			}else{
				List<LogicAllocatInwhItemDO> inItemList = new ArrayList<LogicAllocatInwhItemDO>();
				for(LogicAllocatOutwhItemDO outItem : outItemList){
					if(outItem==null){
						continue;
					}
					LogicAllocatInwhItemDO inItem = new LogicAllocatInwhItemDO();
					inItem.setZd002MaterId(outItem.getZd002MaterId());
					inItem.setZd003Code(outItem.getZd003Code());
					inItem.setZd004Name(outItem.getZd004Name());
					inItem.setZd005Type(outItem.getZd005Type());
					inItem.setZd006Spec(outItem.getZd006Spec());
					inItem.setZd007Model(outItem.getZd007Model());
					inItem.setZd009Color(outItem.getZd009Color());
					inItem.setZd010Quality(outItem.getZd010Quality());
					inItem.setZd011Size(outItem.getZd011Size());
					inItem.setZd012Unit(outItem.getZd012Unit());
					inItem.setZd014Price(outItem.getZd014Price());
					inItem.setZd015TaxPrice(outItem.getZd015TaxPrice());
					//调拨数量
					inItem.setZd018OutQuantity(outItem.getZd017Quantity());
					//已接收数量
					inItem.setZd020HasInQuantity(BigDecimal.ZERO);
					//未接收数量
					inItem.setZd021NoInQuantity(outItem.getZd017Quantity());
					inItemList.add(inItem);
				}
				OutJsonGrid out = new OutJsonGrid(inItemList);
				out.printJSon(response);
			}
		}
	}
}
